package com.rural.bus.driver.adapter;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.rural.bus.driver.MainActivity;
import com.rural.bus.driver.MyApplication;
import com.rural.bus.driver.R;
import com.rural.bus.driver.adapter.base.MyBaseAdapter;
import com.rural.bus.driver.adapter.base.ViewHolder;
import com.rural.bus.driver.model.Order;
import com.rural.bus.driver.model.Railway;
import com.rural.bus.driver.model.User;
import com.rural.bus.driver.net.OnNetCallback;
import com.rural.bus.driver.utils.ProgressUtil;

public class OrderAdapter extends MyBaseAdapter<Order> {
//	public static final int FIELD_STATE=0;

	private List<Order > mOrders;
	private Context mContext;
//	private int mIndex=0;

	public OrderAdapter(Context context,List<Order > orders) {
		super(context,orders);
		this.mOrders=orders;
		this.mContext=context;
	}

	@Override
	public int getCount() {
		return mOrders.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int arg0, View convertView, ViewGroup arg2) {
		if(convertView==null){
			convertView=mInflater.inflate(R.layout.myorder_item, null);
		}
		TextView tvStartLoc=ViewHolder.get(convertView, R.id.tv_start_loc);
		TextView tvEndLoc=ViewHolder.get(convertView, R.id.tv_end_loc);
		TextView tvUsername=ViewHolder.get(convertView, R.id.tv_username);
		TextView tvTime=ViewHolder.get(convertView, R.id.tv_time);
//		TextView tvOrderPaymethod=ViewHolder.get(convertView, R.id.orderpaymethod);
//		tvOrderTime=ViewHolder.get(convertView, R.id.ordertime);
//		TextView tvResponer=ViewHolder.get(convertView, R.id.order_responer);
//		btDelive=ViewHolder.get(convertView, R.id.delive);
		ImageView ivCall=ViewHolder.get(convertView, R.id.iv_call);
      	Button btAction=ViewHolder.get(convertView, R.id.bt_receive);
		
		final Order order=mOrders.get(arg0);
        tvEndLoc.setText(order.endLocation);
        tvStartLoc.setText(order.startLocation);
        tvUsername.setText(order.userName);
        tvTime.setText(""+order.takeTime);
//        String time=DateUtil.getDate(order.getCreatedAt());
//        tvOrderTime.setText(time);
//        mAction.setActionText(btAction);
        switch (order.orderStatus) {
		case Order.ORDERSTATUS_RECEIVED:
			btAction.setText("ready");
			break;
		case Order.ORDERSTATUS_READYED:
			btAction.setText("start");
			break;
		case Order.ORDERSTATUS_DEPARTED:
			btAction.setText("reach");
			break;
		case Order.ORDERSTATUS_REACHED:
			btAction.setText("reached");
			break;
		}
      	ivCall.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+order.userPhone));  
                mContext.startActivity(intent); 
			}
		});
      	
      	btAction.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				switch (order.orderStatus) {
				case Order.ORDERSTATUS_NEW:
					accept(order);
					break;
				case Order.ORDERSTATUS_RECEIVED:
					ready(arg0,order);
					break;
				case Order.ORDERSTATUS_READYED:
					start(arg0, order);
					break;

				case Order.ORDERSTATUS_DEPARTED:
					confirm(arg0, order);
					break;
					
				case Order.ORDERSTATUS_REACHED:
					toast("has reached");
					break;
				}
			}
		});
    	
		return convertView;
	}
	
	/***
	 * 接单
	 * @param order
	 */
	private void accept(final Order order){
//		ProgressUtil.showProgress(mContext, "");
//		MyApplication app=(MyApplication) mContext.getApplicationContext();
//		User user=app.getCurrentUser();
//		final Railway railway=new Railway();
//		railway.accept(mContext,order.userId, order.orderId, new Integer(user.drivId), order.carType, order.orderType, order.number,new OnNetCallback() {
//			
//			@Override
//			public void success(JSONObject object) {
//				ProgressUtil.closeProgress();
//				toast("success");
//				mOrders.remove(order);
//				Order order = railway.order;
//				Intent intent=new Intent(mContext,MainActivity.class);
//				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//				intent.putExtra("order", order);
//				mContext.startActivity(intent);
//			}
//			
//			@Override
//			public void fail(String error) {
//				ProgressUtil.closeProgress();
//				;
//			}
//		});
	}
	
	/***
	 *ready
	 * @param order
	 */
	private void ready(final int index,Order order){
//		ProgressUtil.showProgress(mContext, "");
//		MyApplication app=(MyApplication) mContext.getApplicationContext();
//		
//		final Railway railway=app.getCurrentRailway();
//		railway.ready(mContext, order.orderId, order.groupId, new Integer(app.getCurrentUser().drivId), new OnNetCallback() {
//			
//			@Override
//			public void success(JSONObject object) {
//				toast("success");
//					Order order = railway.order;
////					Log.d("order", "index = "+index);
//					mOrders.set(index, order);
//				ProgressUtil.closeProgress();
//			    notifyDataSetChanged();
//			}
//			
//			@Override
//			public void fail(String error) {
//				ProgressUtil.closeProgress();
//				;
//			}
//		});
	}
	
	/***
	 *start
	 * @param order
	 */
	private void start(final int index,Order order){
//		ProgressUtil.showProgress(mContext, "");
//		MyApplication app=(MyApplication) mContext.getApplicationContext();
//		
//		final Railway railway=app.getCurrentRailway();
//		railway.start(mContext, order.orderId, order.groupId, new Integer(app.getCurrentUser().drivId), new OnNetCallback() {
//			
//			@Override
//			public void success(JSONObject object) {
//				toast("success");
//				Order order = railway.order;
//				mOrders.set(index, order);
//				ProgressUtil.closeProgress();
//			    notifyDataSetChanged();
//			}
//			
//			@Override
//			public void fail(String error) {
//				ProgressUtil.closeProgress();
//				;
//			}
//		});
	}
	
	/***
	 *confirm
	 * @param order
	 */
	private void confirm(final int index,Order order){
//		ProgressUtil.showProgress(mContext, "");
//		MyApplication app=(MyApplication) mContext.getApplicationContext();
//		
//		final Railway railway=app.getCurrentRailway();
//		railway.confirm(mContext, order.orderId, order.groupId, new Integer(app.getCurrentUser().drivId),10, new OnNetCallback() {
//			
//			@Override
//			public void success(JSONObject object) {
//				toast("success");
//				Order order = railway.order;
//				mOrders.set(index, order);
//				ProgressUtil.closeProgress();
//			    notifyDataSetChanged();
//			}
//			
//			@Override
//			public void fail(String error) {
//				ProgressUtil.closeProgress();
//				;
//			}
//		});
	}
	
}
